<?php

namespace App\Http\Controllers\Admin;

use App\Models\Arts\WebVisitModel;
use App\Utils\AppResult;
use Illuminate\Support\Facades\DB;

class IndexController extends ParentController
{

    public function __construct(array $attributes = [])
    {
        parent::__construct();
    }

    public function index()
    {
        return view('admin.index');
    }

    // 获取访问统计
    public function getStatistics()
    {
        $date = date('Y-m-d');// 今日
        $todayTime = strtotime($date);
        $yesterday = date('Y-m-d',strtotime('-1 day'));// 昨日
        $seven_days = [strtotime('-6 days',$todayTime), time()];
        $thirty_days = [strtotime('-29 days',$todayTime), time()];
        $return['today'] = WebVisitModel::query()->where('create_date', $date)->count();
        $return['sevenDays'] = WebVisitModel::query()->whereBetween('created_at', $seven_days)->count();
        $return['thirtyDays'] = WebVisitModel::query()->whereBetween('created_at', $thirty_days)->count();

        $return['ipToday'] = WebVisitModel::query()->where('create_date', $date)->distinct('ip')->count();
        $return['ipYesterday'] = WebVisitModel::query()->where('create_date', $yesterday)->distinct('ip')->count();
        $return['ipSevenDays'] = WebVisitModel::query()->whereBetween('created_at', $seven_days)->distinct('ip')->count();
        $return['ipThirtyDays'] = WebVisitModel::query()->whereBetween('created_at', $thirty_days)->distinct('ip')->count();

        $return['userToday'] = WebVisitModel::query()->where('create_date', $date)->distinct('session_id')->count();
        $return['userYesterday'] = WebVisitModel::query()->where('create_date', $yesterday)->distinct('session_id')->count();
        $return['userSevenDays'] = WebVisitModel::query()->whereBetween('created_at', $seven_days)->distinct('session_id')->count();
        $return['userThirtyDays'] = WebVisitModel::query()->whereBetween('created_at', $thirty_days)->distinct('session_id')->count();

        return AppResult::response200('success', $return);
    }

    // 三十日城市访问排行
    public function getTopList()
    {
        $date = date('Y-m-d');// 今日
        $todayTime = strtotime($date);
        $thirty_days = [strtotime('-29 days',$todayTime), time()];
        $topList = WebVisitModel::query()
            ->whereBetween('created_at', $thirty_days)
            ->where('city','!=','本地')
            ->where('city','!=','未知')
            ->where('city','!=','')
            ->whereNotNull('city')
            ->groupBy('city')
            ->orderBy('num', 'desc')
            ->select([DB::raw('count(Distinct(session_id)) as num'),'city'])->get();
        return AppResult::response200('success', $topList);
    }

    // 获取趋势图
    public function getTrend()
    {
        // 七日数据走势统计
        $date = date('Y-m-d');// 今日
        $todayTime = strtotime($date);
        $seven_days = [strtotime('-6 days',$todayTime), time()];
        $dates = [];
        for ($i = 6; $i >= 0; $i--){
            $dates[] = date('Y-m-d', strtotime('-'.$i.' day'));
        }

        // pv 走势图
        $pvTrend = WebVisitModel::query()
            ->whereBetween('created_at', $seven_days)
            ->groupBy('create_date')
            ->orderBy('create_date','asc')
            ->select([DB::raw('count(*) as num'),'create_date'])->get()->toArray();
        // uv 走势图
        $userTrend = WebVisitModel::query()
            ->whereBetween('created_at', $seven_days)
            ->groupBy('create_date')
            ->orderBy('create_date','asc')
            ->select([DB::raw('count(Distinct(session_id)) as num'),'create_date'])->get()->toArray();
        // ip 走势图
        $ipTrend = WebVisitModel::query()
            ->whereBetween('created_at', $seven_days)
            ->groupBy('create_date')
            ->orderBy('create_date','asc')
            ->select([DB::raw('count(Distinct(ip)) as num'),'create_date'])->get()->toArray();

        $pvTrendNum = [];
        $userTrendNum = [];
        $ipTrendNum = [];
        foreach ($dates as $key=>$date){
            $pvTrendNum[$key] = 0;
            $userTrendNum[$key] = 0;
            $ipTrendNum[$key] = 0;
            foreach ($pvTrend as $k=>$v){
                if ($date == $v['create_date']){
                    $pvTrendNum[$key] = $v['num'];
                }
            }
            foreach ($userTrend as $k=>$v){
                if ($date == $v['create_date']){
                    $userTrendNum[$key] = $v['num'];
                }
            }
            foreach ($ipTrend as $k=>$v){
                if ($date == $v['create_date']){
                    $ipTrendNum[$key] = $v['num'];
                }
            }
            $dates[$key] = date('m/d',strtotime($date));
        }

        return AppResult::response200('success', [
            'dates' => $dates,
            'pvTrendNum' => $pvTrendNum,
            'userTrendNum' => $userTrendNum,
            'ipTrendNum' => $ipTrendNum,
        ]);
    }
}
